package com.liming.auth.controller;

import com.liming.auth.service.AdminAuthService;
import com.liming.auth.service.UserAuthService;
import com.liming.common.pojo.dto.authinfo.AdminAddOrUpdateDTO;
import com.liming.common.pojo.dto.authinfo.AdminLoginDTO;
import com.liming.common.pojo.dto.authinfo.UserPageQuery;
import com.liming.common.pojo.entity.authinfo.Region;
import com.liming.common.pojo.vo.PageVO;
import com.liming.common.pojo.vo.authinfo.*;
import com.liming.common.result.LMResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author liming
 * @date 2025/1/14
 */
@Slf4j
@RestController
@AllArgsConstructor
@Tag(name = "后台权限信息-Service")
@RequestMapping("/service/admin_auth_info")
public class AdminAuthMircoController {

    private final AdminAuthService adminAuthService;
    private final UserAuthService userAuthService;

    @Operation(description = "管理员姓名和手机号")
    @GetMapping("getNameAndPhone/{id}")
    public LMResult<String> getNameAndPhone(@PathVariable Long id) {
        String phoneAndName = adminAuthService.getNameAndPhone(id);
        return LMResult.success(phoneAndName);
    }

    @Operation(description = "列表 - 管理员")
    @GetMapping("list")
    public LMResult<List<AdminVO>> list() {
        List<AdminVO> vo = adminAuthService.listAdminVO();
        return LMResult.success(vo);
    }

    @Operation(description = "权限信息")
    @GetMapping("auth")
    public LMResult<AdminAuthVO> auth() {
        AdminAuthVO vo = adminAuthService.auth();
        return LMResult.success(vo);
    }

    @Operation(summary = "角色列表")
    @GetMapping("roleList")
    public LMResult<List<RoleVO>> roleList() {
        List<RoleVO> result = adminAuthService.roleList();
        return LMResult.success(result);
    }

    @Operation(summary = "权限列表")
    @GetMapping(value = "permissionList")
    public LMResult<List<PermissionVO>> permissionList() {
        List<PermissionVO> result = adminAuthService.permissionList();
        return LMResult.success(result);
    }

    @Operation(summary = "管理员对应角色列表")
    @GetMapping(value = "adminRole/{adminId}")
    public LMResult<List<RoleVO>> adminRoleList(@PathVariable Long adminId) {
        List<RoleVO> result = adminAuthService.adminRoleList(adminId);
        return LMResult.success(result);
    }

    @Operation(summary = "角色对应权限列表")
    @GetMapping(value = "rolePermission/{roleId}")
    public LMResult<List<PermissionVO>> rolePermissionList(@PathVariable Long roleId) {
        List<PermissionVO> result = adminAuthService.rolePermissionList(roleId);
        return LMResult.success(result);
    }

    @Operation(summary = "地区列表")
    @GetMapping(value = "regionList")
    public LMResult<List<Region>> regionList(@RequestParam Long parentId, @RequestParam Integer level) {
        return LMResult.success(adminAuthService.regionList(parentId, level));
    }

    @Operation(summary = "用户分页")
    @GetMapping("userPage")
    public LMResult<PageVO<UserVO>> userPage(UserPageQuery query) {
        return LMResult.success(adminAuthService.userPage(query));
    }

    @Operation(description = "登录")
    @PostMapping("login")
    public LMResult<AdminLoginVO> login(AdminLoginDTO dto) {
        AdminLoginVO vo = adminAuthService.login(dto);
        return LMResult.success(vo);
    }

    @Operation(description = "登出")
    @PostMapping("logout")
    public LMResult<Boolean> logout() {
        return LMResult.success(adminAuthService.logout());
    }

    @Operation(summary = "权限设置 - 添加角色")
    @PostMapping("role")
    public LMResult<Boolean> role(String roleName) {
        return LMResult.success(adminAuthService.addRole(roleName));
    }

    @Operation(summary = "权限设置 - 添加权限")
    @PostMapping("permission")
    public LMResult<Boolean> permission(String name, String description) {
        return LMResult.success(adminAuthService.addPermission(name, description));
    }

    @Operation(description = "权限设置 - 管理员角色")
    @PostMapping("adminRole/{adminId}")
    public LMResult<Boolean> adminRole(@PathVariable Long adminId, @RequestBody List<Long> roleIds) {
        return LMResult.success(adminAuthService.adminRole(adminId, roleIds));
    }

    @Operation(description = "权限设置 - 角色权限")
    @PostMapping("rolePermission/{roleId}")
    public LMResult<Boolean> rolePermission(@PathVariable Long roleId, @RequestBody List<Long> permissionIds) {
        return LMResult.success(adminAuthService.rolePermission(roleId, permissionIds));
    }

    @Operation(summary = "新增 - 管理员")
    @PostMapping
    public LMResult<Boolean> addInfo(@RequestBody AdminAddOrUpdateDTO adminAddOrUpdateDTO) {
        return LMResult.success(adminAuthService.saveAdmin(adminAddOrUpdateDTO));
    }

    @Operation(summary = "更新 - 管理员信息")
    @PutMapping
    public LMResult<Boolean> updateInfo(@RequestBody AdminAddOrUpdateDTO adminAddOrUpdateDTO) {
        return LMResult.success(adminAuthService.updateAdmin(adminAddOrUpdateDTO));
    }

    @Operation(summary = "更新管理员账号状态 0未锁定 1锁定")
    @PutMapping("locked/{id}")
    public LMResult<Boolean> updateStatus(@PathVariable Long id) {
        return LMResult.success(adminAuthService.updateStatus(id));
    }

    @Operation(summary = "更新用户锁定状态 0未锁定 1锁定")
    @PutMapping("userLocked/{id}")
    public LMResult<Boolean> updateUserLocked(@PathVariable Long id) {
        return LMResult.success(userAuthService.updateLockedById(id));
    }

    @Operation(summary = "更新用户激活状态 0未激活 1激活")
    @PutMapping("updateUserActive/{id}")
    public LMResult<Boolean> updateUserActive(@PathVariable Long id) {
        return LMResult.success(userAuthService.updateActiveById(id));
    }

    @Operation(summary = "删除 - 管理员")
    @DeleteMapping("{id}")
    public LMResult<Boolean> delete(@PathVariable Long id) {
        return LMResult.success(adminAuthService.removeById(id));
    }

    @Operation(summary = "删除 - 角色")
    @DeleteMapping("deleteRole/{id}")
    public LMResult<Boolean> deleteRole(@PathVariable Long id) {
        Boolean result = adminAuthService.removeRoleById(id);
        return LMResult.success(result);
    }

    @Operation(summary = "删除 - 权限")
    @DeleteMapping("deletePermission/{id}")
    public LMResult<Boolean> deletePermission(@PathVariable Long id) {
        Boolean result = adminAuthService.removePermissionById(id);
        return LMResult.success(result);
    }


}
